import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
//vue异步组件实现懒加载
// component:resolve=>(require(['@/components/HelloWorld'],resolve))
//es6实现页面懒加载  最常用
// const HelloWorld =()=> import('@/components/HelloWorld')
const Home = () => import('@/views/home/')
const Category = () => import('@/views/category/')
const Cart = () => import('@/views/cart/')
const My = () => import('@/views/my/')
const Productd = () => import('@/views/productd/')
const Theme = () => import('@/views/theme/')
Vue.use(Router)
// vue异步组件实现懒加载
// resolve=>(require(['@/components/HelloWorld'],resolve))
const routes = [
  {
    path: '/', // 默认路由
    redirect: '/home' // 重定向
  },
  {
    path: '/home',
    name: "home",
    component: Home,
    // beforeEnter:(to,from,next)=>{ // 路由独享 ===》 组件守卫
    //   //  home页面被激活
    //   console.log('home--router')
    //   next()
    // },
    meta:{//路由元信息
      title:"主页",
      keepAlive:true,//要缓存
      isTab:true
    }
  },
  {
    path: '/category',
    name: 'category',
    component: Category,
    meta:{
      title:"分类",
      keepAlive:true,//要缓存
      isTab:true
    }
  },
  {
    path: '/cart',
    name: 'cart',
    component: Cart,
    meta:{
      title:"购物车",
      keepAlive:true,
      isTab:true
    }
  },
  {
    path: '/my',
    name: 'my',
    component: My,
    meta:{
      title:"我的",
      keepAlive:true,
      isTab:true
    }
  },
  {
    path: '/productd',
    name: 'productd',
    component: Productd,
    meta:{
      title:"商品详情"
    }
  },
  {
    path: '/theme',
    name: 'theme',
    component: Theme,
    meta:{
      title:"主题商品"
    }
  },
  {
    path:'*',
    redirect:'/'//重定向
  }
]
const router = new Router({
  mode: "history",
  routes: routes
})

router.beforeEach((to, from, next) => { // 导航守卫  监听路由的离开和进入
  // 从from来将要去to
  // to即将要进入的目标的路由对象
  // from当前导航即将要离开的路由对象 
  // next()  调用该方法后，才能进入下一个钩子 
  // console.log('我是全局')
  document.title = to.meta.title
  next()
  //最常见用法： 判断用户有没有登录 
  // if (to.path === '/my') {
  //   next('/') // 
  //   // if (token) { // 登录凭证 
  //   //   next()
  //   // } else {
  //   //   next('/login')
  //   //   // 跳转到登录页面
  //   // }
  // }

})
// router.afterEach((to,from)=>{//路由改变后触发这个钩子
//   console.log(afterEach)
// })
console.log(router)
export default router;
